package com.nari.ncps.screen.service.impl.ne;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.ne.NeProdoperPowerGs;
import com.nari.ncps.screen.mapper.ne.NeOpermonRtPvMapper;
import com.nari.ncps.screen.service.ne.NeOpermonRtPvService;
import com.nari.ncps.screen.vo.ne.NePvRealPowerVO;
import com.nari.ncps.screen.entity.ne.NeOpermonRtPv;
import com.nari.ncps.screen.vo.ne.WindPowerGenerationVO;
import com.nari.ncps.screen.vo.ne.WindRealTimePowerVO;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 光伏实时功率表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class NeOpermonRtPvServiceImpl extends ServiceImpl<NeOpermonRtPvMapper, NeOpermonRtPv> implements NeOpermonRtPvService {

    @Override
    public List<NePvRealPowerVO> getPvRealPower(Integer year) {
        String formattedDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        // 构建查询条件 - 查询最新一天的24小时数据
        LambdaQueryWrapper<NeOpermonRtPv> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NeOpermonRtPv::getDelFlag, 0 );
//        queryWrapper.like(NeOpermonRtPv::getStatisticTime, "2025-10-06" );
        queryWrapper.like(NeOpermonRtPv::getStatisticTime, formattedDate );
        // 先查询最新的日期
        List<NeOpermonRtPv> latestRecord = this.list(queryWrapper);
        if (latestRecord == null) {
            return new ArrayList<>();
        }
        List<NePvRealPowerVO> entities = new ArrayList<>();
        for(NeOpermonRtPv data: latestRecord){
            String dateTimeStr = data.getStatisticTime();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, formatter);
            int newYear = dateTime.getYear();           // 2025
            int month = dateTime.getMonthValue();    // 9
            int day = dateTime.getDayOfMonth();      // 29
            int hour = dateTime.getHour();
            NePvRealPowerVO vo = new NePvRealPowerVO();
            vo.setYear(newYear);
            vo.setMonth(month);
            vo.setDay(day);
            vo.setHour(hour);
            vo.setMinute(0);
//            vo.setDate(data.getStatisticTime());
            vo.setRealtimePower(data.getRealtimePower());
            vo.setTheoreticalPower(data.getTheoreticalPower());
            vo.setIrradiance(data.getIrradiance());
            entities.add(vo);
        }
        return entities;
    }

    @Override
    public WindRealTimePowerVO getWindRealTimePower(String orgCode) {

        if (orgCode == null) {
            orgCode = "134058";
        }
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取年、月、日
        int year = currentDate.getYear();
        int month = currentDate.getMonthValue(); // 1-12
        int day = currentDate.getDayOfMonth();
        LambdaQueryWrapper<NeOpermonRtPv> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NeOpermonRtPv::getOrgCode, orgCode)//根据orgcode来查询是哪个公司的，如果没有传orgcode那么就会按照内蒙公司的维度进行查询
                .eq(NeOpermonRtPv::getDateType, 2)
                .eq(NeOpermonRtPv::getYear, year)
                .eq(NeOpermonRtPv::getMonth, month)
                .eq(NeOpermonRtPv::getDay, day)
                .orderByAsc(NeOpermonRtPv::getHour, NeOpermonRtPv::getMinute);
        List<NeOpermonRtPv> allData = this.list(queryWrapper);
        if (allData != null && !allData.isEmpty()) {
            return new WindRealTimePowerVO();
        }
        List<WindRealTimePowerVO.RealTimePowerItem> list = new ArrayList<>();
        for (NeOpermonRtPv entity : allData){
            WindRealTimePowerVO.RealTimePowerItem item = new WindRealTimePowerVO.RealTimePowerItem();
            item.setTime(entity.getHour() + ":" + entity.getMinute());
            item.setRealTimePower(entity.getRealtimePower());
            list.add(item);
        }
        WindRealTimePowerVO vo = new WindRealTimePowerVO();
        vo.setTrendData(list);
        return vo;
    }

    /**
     * 实体类转换为VO
     */
    private NePvRealPowerVO convertToVO(NeOpermonRtPv entity) {
        NePvRealPowerVO vo = new NePvRealPowerVO();
        vo.setYear(entity.getYear());
        vo.setMonth(entity.getMonth());
        vo.setDay(entity.getDay());
        vo.setDate(entity.getDate());
        vo.setHour(entity.getHour() != null ? entity.getHour().intValue() : null);
        vo.setMinute(entity.getMinute() != null ? entity.getMinute().intValue() : null);
        vo.setRealtimePower(entity.getRealtimePower());
        vo.setTheoreticalPower(entity.getTheoreticalPower());
        vo.setIrradiance(entity.getIrradiance());
        return vo;
    }

}
